using System;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
using ActivityFeed.SharePoint.Sample;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Security;

namespace OpenWaves.ActivityFeed.Sharepoint.Feature.Features.ActivityFeedFeature
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("27930910-1004-47dd-81f8-32c8e1567beb")]
    public class ActivityFeedFeatureEventReceiver : ActivityFeedFeatureReceiverBase
    {
        protected override bool CanAddList(SPList list)
        {
            return list.BaseTemplate == SPListTemplateType.DocumentLibrary ||
                list.BaseTemplate == SPListTemplateType.WebPageLibrary ||
                list.BaseTemplate == SPListTemplateType.Tasks;
        }

        protected override string GetActivityFeedConectionString()
        {
            return "Url=http://localhost:8080";
        }

        protected override Type GetEventReceiverClassName()
        {
            return typeof(ActivityFeedEventReceiver);
        }

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            base.FeatureActivated(properties);

            try
            {
                /*
                 
                 -- handler
  <location path="activityFeed">
    <system.webServer>
      <modules runAllManagedModulesForAllRequests="true" />
      <validation validateIntegratedModeConfiguration="false" />
      <handlers>
        <add name="ActivityFeed" verb="*" type="OpenWaves.ActivityFeed.Web.Handler.ActivityFeedApiHandler, OpenWaves.ActivityFeed.Web,  Version=1.0.79.488, Culture=neutral, PublicKeyToken=c2f044223b736562" path="*" />
      </handlers>
    </system.webServer>
  </location>
  <location path="_layouts/images">
    <system.web>
      <authorization>
        <allow users="*" />
      </authorization>
    </system.web>
  </location>

                  --- module
                 <add name="DocumentSessionPerRequestModule" type="OpenWaves.ActivityFeed.Web.DocumentSessionPerRequestHttpModule, OpenWaves.ActivityFeed.Web,  Version=1.0.79.488, Culture=neutral, PublicKeyToken=c2f044223b736562" preCondition="managedHandler" />
                
                 
                 Problem: http://learningmoss.blogspot.com/2009/11/resolve-webconfigmodifications-in.html
                */


                //this.SetConnectionStirngForWebPart(properties);
            }
            catch (SecurityException securityException)
            {
                SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("ActivityFeed", TraceSeverity.Unexpected, EventSeverity.Error),
                                TraceSeverity.Unexpected, securityException.Message, securityException.StackTrace);
            }
        }
        /*
        private void SetConnectionStirngForWebPart(SPFeatureReceiverProperties properties)
        {
            SPWebApplication app;
            var siteCollection = properties.Feature.Parent as SPSite;
            if (siteCollection != null)
            {
                app = siteCollection.WebApplication;
            }
            else
            {
                var site = properties.Feature.Parent as SPWeb;
                if (site == null)
                {
                    throw new SPException("Cannot setup connectionstring");
                }
                app = site.Site.WebApplication;
            }

            var configModificator = new ConfigModificator(app.WebConfigModifications);

            var assemblyName = typeof(OpenWaves.ActivityFeed.Web.DocumentSessionPerRequestHttpModule).Assembly.FullName;

            // module
            configModificator.EnsureSection("system.webServer", "configuration");
            configModificator.EnsureSection("modules", "configuration/system.webServer");
            configModificator.EnsureChildNode(
                "configuration/system.webServer/modules",
                "add[@name='DocumentSessionPerRequestModule']",
                string.Format(CultureInfo.InvariantCulture,
                              "<add name='DocumentSessionPerRequestModule' type='OpenWaves.ActivityFeed.Web.DocumentSessionPerRequestHttpModule, {0}' preCondition='managedHandler' />",
                              assemblyName)
                );


            // activity path handler
            configModificator.EnsureSection("location[@path='activityFeed']", "configuration");
            configModificator.EnsureSection("system.webServer", "configuration/location[@path='activityFeed']");

            configModificator.EnsureChildNode(
                "configuration/location[@path='activityFeed']/system.webServer",
                "modules[@runAllManagedModulesForAllRequests='true']",
                "<modules runAllManagedModulesForAllRequests='true' />"
                );

            configModificator.EnsureChildNode(
                "configuration/location[@path='activityFeed']/system.webServer",
                "validation[@validateIntegratedModeConfiguration='false']",
                "<validation validateIntegratedModeConfiguration='false' />"
                );

            configModificator.EnsureSection("handlers", "configuration/location[@path='activityFeed']/system.webServer");
            configModificator.EnsureChildNode(
                "configuration/location[@path='activityFeed']/system.webServer/handlers",
                "add[@name='ActivityFeed']",
                string.Format(CultureInfo.InvariantCulture, "<add name='ActivityFeed' verb='*' type='OpenWaves.ActivityFeed.Web.Handler.ActivityFeedApiHandler, {0}' path='*' />", assemblyName)
                );


            var service = SPFarm.Local.Services.GetValue<SPWebService>();
            app.Update();
            service.ApplyWebConfigModifications();
        }
        */
    }
}
